package com.guo.hui.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.guo.hui.dao.blog.TpTimelineMapper;
import com.guo.hui.dao.redis.RedisCacheUtil;
import com.guo.hui.model.blog.TpTimeline;
import com.guo.hui.model.shiro.TpUser;
import com.guo.hui.service.TimeLineService;

@Service("timeLineService")
public class TimeLineServiceImpl implements TimeLineService {

	@Autowired
	private TpTimelineMapper timeLineDao;

	@Override
	public List<TpTimeline> queryByMap(Map<String, Object> map) {
		List<TpTimeline> list = timeLineDao.selectByMap(map);
//		List<TpTimeline> list = (List<TpTimeline>)RedisCacheUtil.get("timeline");;
//		if(list==null||list.size()==0){
//			list = timeLineDao.selectByMap(map);
//			RedisCacheUtil.setTime("timeline", list, 30);
//			System.out.println("----------");
//		}
		return list;
	}

	@Override
	public void saveline(String title, String content, Integer id, TpUser user) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String time = format.format(new Date());
		if (id == null) {
			TpTimeline tp = new TpTimeline();
			tp.setTitle(title);
			tp.setContent(content);
			tp.setTime(time);
			tp.setUsercode(user.getUsercode());
			tp.setUsername(user.getUsername());
			tp.setTimeupdate(new Date());
			timeLineDao.insert(tp);
		} else {
			TpTimeline tp = timeLineDao.selectById(id);
			tp.setTitle(title);
			tp.setContent(content);
			tp.setUsercode(user.getUsercode());
			tp.setUsername(user.getUsername());
			tp.setTimeupdate(new Date());
			timeLineDao.update(tp);
		}

	}

}
